package cn.com.checne.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.checne.model.domain.JobDept;
import cn.com.checne.model.domain.LanguageInfo;


@Repository
public class JobDeptDao {
	protected static final Logger log = Logger.getLogger(JobDeptDao.class);

	/**
	 * create
	 * @param dept
	 * @return
	 */
	public boolean create(JobDept dept) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String createSql = "insert into tbl_job_dept(dept_name,dept_desc,dept_order,lg_id_fk) values(?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(createSql);
			int i = 0 ;
			pstmt.setString(++i,dept.getName());
			pstmt.setString(++i,dept.getDesc());
			pstmt.setInt(++i,dept.getOrder());
			pstmt.setInt(++i,dept.getLanguageId());
			pstmt.execute();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param dept
	 * @return
	 */
	public boolean update(JobDept dept) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "update tbl_job_dept set dept_name = ?,dept_desc = ? ,dept_order = ?,lg_id_fk = ? where dept_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setString(++i,dept.getName());
			pstmt.setString(++i,dept.getDesc());
			pstmt.setInt(++i,dept.getOrder());
			pstmt.setInt(++i,dept.getLanguageId());
			pstmt.setInt(++i,dept.getDeptId());
			log.info("[SQL]" + pstmt.toString());
			pstmt.executeUpdate();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param id
	 * @return
	 */
	public boolean delete(int id){
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "delete from tbl_job_dept where dept_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setInt(++i,id);
			pstmt.executeUpdate();
			log.info("[SQL]" + pstmt.toString());
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result ;
	}
	
	/**
	 * 
	 * @param deptId
	 * @return
	 */
	public JobDept getJobDept(int deptId) {
		JobDept dept = new JobDept();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_job_dept where dept_id = ? ";

			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, deptId);
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			List<JobDept> list = getResult(rs);
			dept = (list != null && list.size() > 0 ) ? list.get(0) : dept ;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dept;
	}
	/**
	 * 
	 * @return
	 */
	public List<JobDept> getAll(int languageId) {
		List<JobDept> list = new ArrayList<JobDept>();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_job_dept " ;
			if(languageId > 0){
				sql += " where lg_id_fk = ? " ;
			}
			sql += "order by dept_order asc ";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			if(languageId > 0){
				pstmt.setInt(1, languageId);
			}
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			list.addAll(getResult(rs));
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * get rseult from resultSet
	 * @param rs
	 * @return
	 */
	public List<JobDept> getResult(ResultSet rs) {
		List<JobDept> list = new ArrayList<JobDept>();
		try {
			JobDept dept = null ;
			while(rs.next()) {
				dept = new JobDept();
				dept.setDeptId(rs.getInt("dept_id"));
				dept.setName(rs.getString("dept_name"));
				dept.setDesc(rs.getString("dept_desc"));
				dept.setOrder(rs.getInt("dept_order"));
				dept.setLanguageId(rs.getInt("lg_id_fk"));
				LanguageInfo lg = new LanguageInfoDao().getLanguageInfoById(dept.getLanguageId());
				dept.setLanguageInfo(lg);
				list.add(dept);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}